__author__ = 'hashaki'
# 单变量线性回归
import numpy as np
import math
import matplotlib.pyplot as plt

path = 'F:\强化学习\机器学习\JiQiXueXisample\线性回归/data1.txt'
data = np.loadtxt(path, delimiter=',')

w = 0.0
b = 0.0
learning_rate = 75
iter_num = 1000
x = []
y = []
for i in data:
    x.append(i[0])
    y.append(i[1])

x_max=np.max(x)
x_min=np.min(x)
sigmoid = lambda x: 1/(1-math.exp(x))

N = len(data)
for i in range(iter_num):
    for j in range(N):
        _x = data[j, 0]
        _y = data[j, 1]
        w += 2*_x * sigmoid(_y - (w * _x + b))
        b += 2*sigmoid(_y - (w * _x + b))
    w=-(1 / N) * w*learning_rate
    b=-(1 / N) * b
y_predict = []
for k in x:
    y_predict.append(w * k - b)
plt.figure()
plt.scatter(x, y)
plt.plot(x, y_predict, 'b')
plt.show()
